package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.service.ResourceService;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.awt.SunHints;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("resource")
@ResponseBody
public class ResourceController {
    @Resource
    private ResourceService resourceService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<com.kkb.pojo.Resource> selectResourcesInPage(@RequestParam("index") int index,
                                                                 @RequestParam("size") int size,
                                                                 @RequestParam("name") String name){
        PageInfo<com.kkb.pojo.Resource> resourcePageInfo = resourceService.selectResourceInPages(index, size, name);
        return new ResultVO<>(resourcePageInfo);
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVO<com.kkb.pojo.Resource> addResource(com.kkb.pojo.Resource resource){
        int i = resourceService.addResource(resource);
        if (i>0){
            ResultVO<com.kkb.pojo.Resource> objectResultVO = new ResultVO<>();
            System.out.println(objectResultVO);
            return objectResultVO;
        }
        return new ResultVO<>(500,"添加失败！");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVO<com.kkb.pojo.Resource> selectResourceInfo(@PathVariable("id") Integer resourceId){
        com.kkb.pojo.Resource resource = resourceService.selectResourceInfo(resourceId);
        if (resource!=null){
            return  new ResultVO<>(resource);
        }
        return new ResultVO<>(404,"未查询到此资源信息");
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVO<com.kkb.pojo.Resource> updateResource(com.kkb.pojo.Resource resource){
        int i = resourceService.updateResource(resource);
        if (i>0){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"服务器出现异常");
    }


    @RequestMapping(value = "parent",method = RequestMethod.GET)
    public ResultVO<com.kkb.pojo.Resource> selectParentList(){
        List<com.kkb.pojo.Resource> resources = resourceService.selectParentList();
        if (resources!=null) {
            return new ResultVO<com.kkb.pojo.Resource>(resources);
        }
        return new ResultVO<>(404,"暂无菜单资源");
    }


    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVO<com.kkb.pojo.Resource> deleteResource(@PathVariable("id") Integer id){
        int i = resourceService.logicalDeleteResource(id);
        if (i>0){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"服务器异常");
    }

    @RequestMapping(value = "list",method = RequestMethod.DELETE)
    public ResultVO deleteSelected(@RequestParam("check") List<Integer> check){
        //check.forEach(check1 -> System.out.println(check1));
        int i = resourceService.logicDeleteResourceList(check);
        if (i>0){
            return new ResultVO();
        }
        return new ResultVO(500,"服务器异常！");
    }

}
